package top.lyc829.iot_android.fragment.log;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gturedi.views.StatefulLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.model.ApiResult;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.List;

import butterknife.BindView;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.adapter.LogAdapter;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.core.LazyFragment;
import top.lyc829.iot_android.entity.Log;
import top.lyc829.iot_android.entity.PageInfo;
import top.lyc829.iot_android.entity.User;
import top.lyc829.iot_android.http.service.LogService;
import top.lyc829.iot_android.http.service.UserService;
import top.lyc829.iot_android.http.subscriber.NoTipRequestSubscriber;
import top.lyc829.iot_android.http.subscriber.TipProgressLoadingSubscriber;
import top.lyc829.iot_android.util.Utils;

@Page(name = "LogFragment")
public class LogFragment extends LazyFragment {

    @BindView(R.id.log_recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_stateful)
    StatefulLayout mLlStateful;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private LogAdapter mLogAdapter;

    private PageInfo pageInfo = new PageInfo();

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setTitle("记录")
                .disableLeftView();
        return titleBar;
    }

    public static LogFragment newInstance() {
        return new LogFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.log_fragment;
    }

    @Override
    protected void initViews() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mLogAdapter = new LogAdapter());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    public void lazyInit() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                pageInfo.reset();
                mLlStateful.showLoading();
                getLogList(refreshLayout);
            }
        });

        mRefreshLayout.autoRefresh();

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getLogList(refreshLayout);
            }
        });
    }

    @Override
    protected void initListeners() {


    }

    private void getLogList(RefreshLayout refreshLayout) {

        CustomRequest request = XHttp.custom();

        request.apiCall(request.create(LogService.class).getLogs(pageInfo.getPage()))
                .subscribeWith(new NoTipRequestSubscriber<List<Log>>() {
                    @Override
                    protected void onSuccess(List<Log> logs) {
                        mLlStateful.showContent();
                        if (pageInfo.isFirstPage()) {
                            mLogAdapter.setList(logs);
                            refreshLayout.finishRefresh(true);

                        } else {
                            mLogAdapter.addData(logs);
                        }
                        if (logs == null || logs.size() < PageInfo.MAX_ITEM) {
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            refreshLayout.finishLoadMore();
                            pageInfo.nextPage();
                        }

                    }

                    @Override
                    public void onError(ApiException e) {
                        refreshLayout.finishRefresh(false);
                        mLlStateful.showError(e.getMessage(), v -> {
                            getLogList(refreshLayout);
                        });
                    }
                });

    }
}